<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="./lodash.min.js"></script>
    <script>
        let numberAry = ["3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"];
        let symbolAry = ["方块", "梅花", "红桃", "黑桃"];

        // 1. 生成一副完整的牌（54）自己添加大小王
        const fullPokers = createFullPoker();

        function createFullPoker() {
            let pokers = []; // 保存完整的54张牌
            numberAry.forEach((numberItem, numberIndex) => {
                symbolAry.forEach((symbolItem, symbolIndex) => {
                    pokers.push({
                        text: symbolItem + numberItem,
                        i: numberIndex,
                        j: symbolIndex
                    });
                })
            })

            pokers.push({
                text: '小王',
                i: numberAry.length,
                j: symbolAry.length
            }, {
                text: '大王',
                i: numberAry.length + 1,
                j: symbolAry.length + 1
            })

            return pokers;

        }



        // 2. 发牌
        // （方式一：每一次都随机取一个下标） let index = random(0, 53)
        function createPlayer() {
            let player = [];
            for (let i = 0; i < 17; i++) {
                const index = random(0, fullPokers.length - 1);
                // player.push(fullPokers.splice(index, 1)[0]);
                const deleteResult = fullPokers.splice(index, 1);
                player.push(deleteResult[0]);
            }
            return player;
        }

        const player1 = pokersSort(createPlayer());
        const player2 = pokersSort(createPlayer());
        const player3 = pokersSort(createPlayer());
        const cards = pokersSort(fullPokers);


        // 3. 排序
        function pokersSort(poker) {
            poker.sort((a, b) => {
                if (a.i === b.i) {
                    return a.j - b.j;
                }
                return a.i - b.i;
            })
            return poker;
        }



        console.log(player1);
        console.log(player2);
        console.log(player3);
        console.log(cards);




        function random(min, max) {
            if (!max) {  // 当max没有值的时候
                [max, min] = [min, 0];
            }
            return parseInt(Math.random() * (max - min + 1) + min);
        }
    </script>
</body>

</html>